package com.mathassistant.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.mathassistant.client.presenter.ExercisePresenter.Display;
import com.mathassistant.shared.ExerciseDTO;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;

public class ExerciseView extends Composite implements Display{

	private static ExerciseViewUiBinder uiBinder = GWT
			.create(ExerciseViewUiBinder.class);
	@UiField HTML questionHTML;
	@UiField DecoratorPanel anwserPanel;
	@UiField RadioButton radioA;
	@UiField RadioButton radioB;
	@UiField RadioButton radioC;
	@UiField RadioButton radioD;
	@UiField HTML anwserD_HTML;
	@UiField HTML anwserC_HTML;
	@UiField HTML anwserB_HTML;
	@UiField HTML anwserA_HTML;
	@UiField Button checkButton;
	@UiField Image image;
	@UiField Button nextButton;
	@UiField Label exercisePerTotal;

	String corrImageUrl = "./Images/face-smiley.gif";
	String incorrImageUrl = "./Images/face-sad.gif";
	
	
	interface ExerciseViewUiBinder extends UiBinder<Widget, ExerciseView> {
	}

	public ExerciseView() {
		initWidget(uiBinder.createAndBindUi(this));
		image.setVisible(false);
		nextButton.setVisible(false);
		checkButton.setVisible(false);
	}

	@Override
	public HasClickHandlers getAnswerRadioButton1() {
		// TODO Auto-generated method stub
		return radioA;
	}

	@Override
	public HasClickHandlers getAnswerRadioButton2() {
		// TODO Auto-generated method stub
		return radioB;
	}

	@Override
	public HasClickHandlers getAnswerRadioButton3() {
		// TODO Auto-generated method stub
		return radioC;
	}

	@Override
	public HasClickHandlers getAnswerRadioButton4() {
		// TODO Auto-generated method stub
		return radioD;
	}

	@Override
	public HasClickHandlers getCheckButton() {
		// TODO Auto-generated method stub
		return checkButton;
	}

	@Override
	public HasClickHandlers getNextButton() {
		// TODO Auto-generated method stub
		return nextButton;
	}

	@Override
	public void radioSelected() {
		// TODO Auto-generated method stub
		if(!checkButton.isVisible())
			checkButton.setVisible(true);
	}
	
	@Override
	public void toCorrectView() {
		// TODO Auto-generated method stub
		checkButton.setVisible(false);
		image.setVisible(true);
		image.setUrl(corrImageUrl);
		nextButton.setVisible(true);
	}

	@Override
	public void toWrongView() {
		// TODO Auto-generated method stub
		image.setVisible(true);
		image.setUrl(incorrImageUrl);
	}

	@Override
	public void toEmptyView() {
		// TODO Auto-generated method stub
		anwserPanel.setVisible(false);
		questionHTML.setHTML("Chưa có bài tập nào! ^^");
	}

	@Override
	public void toFinishView() {
		// TODO Auto-generated method stub
		anwserPanel.setVisible(false);
		questionHTML.setHTML("Hoàn thành chủ đề");
	}

	@SuppressWarnings("deprecation")
	@Override
	public void setData(ExerciseDTO exercise) {
		// TODO Auto-generated method stub
		try{
			questionHTML.setHTML(exercise.getQuestion());
			anwserA_HTML.setHTML(exercise.getChooses().get(0));
			anwserB_HTML.setHTML(exercise.getChooses().get(1));
			anwserC_HTML.setHTML(exercise.getChooses().get(2));
			anwserD_HTML.setHTML(exercise.getChooses().get(3));
			radioA.setChecked(false);
			radioB.setChecked(false);
			radioC.setChecked(false);
			radioD.setChecked(false);
			image.setVisible(false);
			nextButton.setVisible(false);
			checkButton.setVisible(false);
			
		}catch (Exception e) {
			// TODO: handle exception
			Window.alert("Dữ liệu bài tập bị lỗi.");
		}
	}

	@Override
	public void setExPerTotal(String indexPerTotal) {
		// TODO Auto-generated method stub
		exercisePerTotal.setText(indexPerTotal);
	}
	
	@Override
	public Widget asWidget() {
		// TODO Auto-generated method stub
		return this;
	}
}
